package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage (qo.getCurrentPage (), qo.getPageSize ());
        List<User> list = userMapper.selectForList (qo);
        return new PageInfo<> (list);
    }

    public User getByUserId(Long id) {
        return userMapper.selectByPrimaryKey (id);
    }

    public Map<String, Object> getCurrentIdByTargetId(Long currentId, Long targetId) {
        return userMapper.getCurrentIdByTargetId (currentId, targetId);
    }

    public void save(Long currentId, Long targetId) {
        userMapper.insertFollow (currentId, targetId);
    }

    public void delete(Long currentId, Long targetId) {
        userMapper.deleteRelation (currentId, targetId);

    }

    //zbj修改了该方法
    public int updateFansNum(Long targetId) {
        userMapper.updateFansNum(targetId);
        return userMapper.selectByPrimaryKey(targetId).getFansNum();
    }


    @Override
    public void updateFollowNum(Long currentId) {
        userMapper.updateFollowNum (currentId);
    }

    //zbj修改了该方法
    public int removeFansNum(Long targetId) {
        userMapper.removeFansNum(targetId);
        return userMapper.selectByPrimaryKey(targetId).getFansNum();
    }


    public void removeFollowNum(Long currentId) {
        userMapper.removeFollowNum (currentId);
    }

    @Override
    public User getUserScore(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public User login(String email, String password) {
        //1:调用Mapper查询数据库
        User u = userMapper.selectByInfo (email, password);
        //2:判断对象是否为null
        if (u == null) {
            throw new RuntimeException ("邮箱和密码不匹配");
        }
        return u;
    }

    public void regist(User u) {
        //1:检查当前用户是否存在
        User user = userMapper.selectByEmail (u.getEmail ());
        if (user != null) { //该用户已经存在,抛出异常通知调用者
            throw new RuntimeException ("该用户已经存在");
        }
        //2:不存在才能注册
        saveOrUpdate(u);
    }

    public void saveOrUpdate(User u) {
        if (u.getId() == null) {
            //前台只会传入邮箱/昵称/密码,剩下的信息在代码给默认值
            u.setPlace ("中国");
            u.setHeadImgUrl ("/img/user/head.jpg");
            u.setCoverImgUrl ("/img/user/bg.jpeg");
            u.setGender (User.GENDER_NONE);
            u.setSign ("我爱玩玩玩");
            userMapper.insert (u);
        } else {
            userMapper.updateByPrimaryKey(u);
        }
    }
}
